﻿using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace SenGe.Domain
{
    /// <summary>
    /// 系统验证类集合
    /// </summary>
    internal class SysValidation
    {
    }

    /// <summary>
    /// 手机号码验证
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class PhoneNumberValidationAttribute : ValidationAttribute
    {
        private const string DefaultErrorMessage = "手机号码格式不正确！";

        public PhoneNumberValidationAttribute() : base(DefaultErrorMessage) { }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value == null) return ValidationResult.Success;
            string? phoneNumber = value.ToString();
            if (string.IsNullOrWhiteSpace(phoneNumber)) return ValidationResult.Success;
            // 中国的手机号码通常以1开头，长度为11位
            var pattern = @"^1[3-9]\d{9}$";
            if (!Regex.IsMatch(phoneNumber, pattern))
            {
                return new ValidationResult(ErrorMessageString);
            }
            return ValidationResult.Success;
        }
    }

    /// <summary>
    /// 用户昵称验证
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class NameValidationAttribute : ValidationAttribute
    {
        private const string DefaultErrorMessage = "用户昵称必须以中英文字符开头，可包含中英文和数字！";

        public NameValidationAttribute() : base(DefaultErrorMessage) { }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value == null) return new ValidationResult(ErrorMessageString);
            string? nickname = value.ToString();
            if (string.IsNullOrWhiteSpace(nickname)) return new ValidationResult(ErrorMessageString);
            // 验证用户昵称必须以中英文字符开头，可包含中英文和数字
            var pattern = @"^[\u4e00-\u9fa5a-zA-Z][\u4e00-\u9fa5a-zA-Z0-9]*$";
            if (!Regex.IsMatch(nickname, pattern))
            {
                return new ValidationResult(ErrorMessageString);
            }
            return ValidationResult.Success;
        }
    }

    /// <summary>
    /// 用户登录名验证
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class UserNameValidationAttribute : ValidationAttribute
    {
        private const string DefaultErrorMessage = "用户名必须以英文字符开头，可包含英文、数字和下划线(_)！";

        public UserNameValidationAttribute() : base(DefaultErrorMessage) { }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value == null) return new ValidationResult(ErrorMessageString);
            string? username = value.ToString();
            if (string.IsNullOrWhiteSpace(username)) return new ValidationResult(ErrorMessageString);
            // 验证用户登录名必须以英文字符开头，可包含英文、数字和下划线(_)
            var pattern = @"^[a-zA-Z][a-zA-Z0-9_]*$";
            if (!Regex.IsMatch(username, pattern))
            {
                return new ValidationResult(ErrorMessageString);
            }
            return ValidationResult.Success;
        }
    }
}